function main()
{
	Turbo 20
	extension -require isxvg

	declare items settingsetref script
	declare npcs settingsetref script
	declare RenderCount int script

	LavishSettings:AddSet[VGDBASE]
	LavishSettings[VGDBASE]:AddSet[items]
	LavishSettings[VGDBASE]:AddSet[npcs]

	items:Set[${LavishSettings[VGDBASE].FindSet[items]}]
	npcs:Set[${LavishSettings[VGDBASE].FindSet[npcs]}]

	; Load values from XML if present
	LavishSettings[VGDBASE]:Import[${Script[database].CurrentDirectory}/DB_DATA/dbase.xml]

	call ScanInventory
	call ScanPawns

	Event[VG_OnAddInventoryItem]:AttachAtom[VG_onAddInventoryItem]
	Event[VG_onPawnSpawned]:AttachAtom[VG_onPawnSpawned]

	while 1
	{
		
		waitframe
	}
}

function ScanInventory()
{
	variable int i = ${Me.Inventory}
	while ${Me.Inventory[${i}](exists)}
	{
		if !${items.FindSetting[${Me.Inventory[${i}].ID}](exists)}
		{
			items:AddSetting[${Me.Inventory[${i}].ID},"${Me.Inventory[${i}].ID}::${Me.Inventory[${i}].IconID}::${Me.Inventory[${i}].Index}::${Me.Inventory[${i}].Name.Escape}::${Me.Inventory[${i}].ToLink.Escape}::${Me.Inventory[${i}].Description.Escape}::${Me.Inventory[${i}].MiscDescription.Escape}::${Me.Inventory[${i}].Type}::${Me.Inventory[${i}].Rarity}::${Me.Inventory[${i}].DefaultEquipSlot}::${Me.Inventory[${i}].Weight}::${Me.Inventory[${i}].NumSlots}::${Me.Inventory[${i}].Keyword1}::${Me.Inventory[${i}].Keyword2}::${Me.Inventory[${i}].Flags}::${Me.Inventory[${i}].MaxQuantity}::${Me.Inventory[${i}].Level}::${Me.Inventory[${i}].DefaultDmg}::${Me.Inventory[${i}].Speed}::${Me.Inventory[${i}].ArmorType}::${Me.Inventory[${i}].ArmorClassMod}::${Me.Inventory[${i}].ArmorClassModMax}::${Me.Inventory[${i}].RangedWeaponType}"]
		}
		i:Dec
	}
}

function ScanPawns()
{
	variable int i = ${VG.PawnCount}
	while ${Pawn[${i}](exists)}
	{
		if !${Pawn[${i}].Type.Equal[PC]} && !${Pawn[${i}].Type.Equal[Me]} && !${Pawn[${i}].Type.Equal[Pet]} && !${Pawn[${i}].Type.Equal[MyPet]} && !${Pawn[${i}].Type.Equal[Group Member]} && !${Pawn[${i}].Type.Equal[Corpse]} && !${Pawn[${i}].Type.Equal[Mailbox]}
		{
			if !${npcs.FindSetting[${Pawn[${i}].Name}](exists)}
			{
				npcs:AddSetting[${Pawn[${i}].Name},"${Pawn[${i}].ID}::${Pawn[${i}].Name.Escape}::${Pawn[${i}].Type}::${Pawn[${i}].X}::${Pawn[${i}].Y}::${Pawn[${i}].Z}::${Pawn[${i}].Level}::${Pawn[${i}].Gender}::${Pawn[${i}].TrueGender}::${Pawn[${i}].Race}::${Pawn[${i}].RaceAbr}::${Pawn[${i}].FactionID}::${Pawn[${i}].MaxSpeed}::${Pawn[${i}].CanSeeMeIfStealthed}::${Pawn[${i}].IsStealthed}::${Pawn[${i}].IsAggro}::${Pawn[${i}].CanFly}::${Pawn[${i}].CanSwim}::${Pawn[${i}].CullDistance}"]
			}
		}
		i:Dec
	}
}

function atexit()
{
	LavishSettings[VGDBASE]:Export[${Script[database].CurrentDirectory}/DB_DATA/dbase.xml]
	LavishSettings[VGDBASE]:Clear
	LavishSettings[VGDBASE]:Remove
}

atom(script) VG_onAddInventoryItem(string ItemName, int ItemID, int Level, string Type, string Keyword1, string Keyword2, string MiscDescription)
{
	call ScanInventory
}

atom(script) VG_onPawnSpawned(string PID, string PName, string PLevel, string PType)
{
	if !${Pawn[ID,${PID}].Type.Equal[PC]} && !${Pawn[ID,${PID}].Type.Equal[Me]} && !${Pawn[ID,${PID}].Type.Equal[Pet]} && !${Pawn[ID,${PID}].Type.Equal[MyPet]} && !${Pawn[ID,${PID}].Type.Equal[Group Member]} && !${Pawn[ID,${PID}].Type.Equal[Corpse]} && !${Pawn[ID,${PID}].Type.Equal[Mailbox]}
	{
		if !${npcs.FindSetting[${Pawn[ID,${PID}].Name}](exists)}
		{
			npcs:AddSetting[${Pawn[ID,${PID}].Name},"${Pawn[ID,${PID}].ID}::${Pawn[ID,${PID}].Name.Escape}::${Pawn[ID,${PID}].Type}::${Pawn[ID,${PID}].X}::${Pawn[ID,${PID}].Y}::${Pawn[ID,${PID}].Z}::${Pawn[ID,${PID}].Level}::${Pawn[ID,${PID}].Gender}::${Pawn[ID,${PID}].TrueGender}::${Pawn[ID,${PID}].Race}::${Pawn[ID,${PID}].RaceAbr}::${Pawn[ID,${PID}].FactionID}::${Pawn[ID,${PID}].MaxSpeed}::${Pawn[ID,${PID}].CanSeeMeIfStealthed}::${Pawn[ID,${PID}].IsStealthed}::${Pawn[ID,${PID}].IsAggro}::${Pawn[ID,${PID}].CanFly}::${Pawn[ID,${PID}].CanSwim}::${Pawn[ID,${PID}].CullDistance}"]
		}
	}
}